package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.app.domain.AppRecord;
import com.ruoyi.app.domain.bo.AppDynamicCommentBo;
import com.ruoyi.app.domain.vo.AppDynamicVo;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.enums.NoticType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppTeamCommentBo;
import com.ruoyi.app.domain.vo.AppTeamCommentVo;
import com.ruoyi.app.domain.AppTeamComment;
import com.ruoyi.app.mapper.AppTeamCommentMapper;
import com.ruoyi.app.service.IAppTeamCommentService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * APP用户动态评论Service业务层处理
 *
 * @author heike
 * @date 2023-08-18
 */
@RequiredArgsConstructor
@Service
public class AppTeamCommentServiceImpl implements IAppTeamCommentService {

    private final AppTeamCommentMapper baseMapper;

    /**
     * 查询APP用户动态评论
     */
    @Override
    public AppTeamCommentVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询APP用户动态评论列表
     */
    @Override
    public TableDataInfo<AppTeamCommentVo> queryPageList(AppTeamCommentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppTeamComment> lqw = buildQueryWrapper(bo);
        Page<AppTeamCommentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询APP用户动态评论列表
     */
    @Override
    public List<AppTeamCommentVo> queryList(AppTeamCommentBo bo) {
        LambdaQueryWrapper<AppTeamComment> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(AppTeamComment::getCreateTime);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppTeamComment> buildQueryWrapper(AppTeamCommentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppTeamComment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTeamId() != null, AppTeamComment::getTeamId, bo.getTeamId());
        return lqw;
    }

    /**
     * 新增APP用户动态评论
     */
    @Override
    public Boolean insertByBo(AppTeamCommentBo bo) {
        AppTeamComment add = BeanUtil.toBean(bo, AppTeamComment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改APP用户动态评论
     */
    @Override
    public Boolean updateByBo(AppTeamCommentBo bo) {
        AppTeamComment update = BeanUtil.toBean(bo, AppTeamComment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppTeamComment entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除APP用户动态评论
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public AppTeamCommentBo addComment(Long id, Long userId, String content) {
        AppTeamCommentBo appTeamCommentBo = new AppTeamCommentBo();
        appTeamCommentBo.setTeamId(id);
        appTeamCommentBo.setContext(content);
        appTeamCommentBo.setUserId(userId);
        insertByBo(appTeamCommentBo);
        return appTeamCommentBo;
    }

    @Override
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }
}
